import 'package:flutter/material.dart';
import 'package:flutter_douban/config/Http_service.dart';
import 'package:flutter_douban/config/app_colors.dart';
import 'package:flutter_douban/model/cook_info_model.dart';
import 'package:flutter_douban/tool_views/cached_network_Image.dart';
import 'package:flutter_douban/tool_views/stance_page.dart';

import 'cook_detail_page.dart';

class AuthorCollectionPage extends StatefulWidget {
  final int id;

  const AuthorCollectionPage({Key? key,required this.id}) : super(key: key);

  @override
  _AuthorCollectionPageState createState() => _AuthorCollectionPageState();
}

class _AuthorCollectionPageState extends State<AuthorCollectionPage>
    with AutomaticKeepAliveClientMixin {
  /// 列表数组模型
  List<CookInfoModel> _cookInfoList = CookInfoModelList([]).list;

  // 获取数据
  Future _getRecommendData({bool replace = true}) async {
    try {
      /// 全部数据
      Map<String, dynamic> result =
          await HttpService.getAuthorCollectionData(id: widget.id);
      CookInfoModelList infoList =
          CookInfoModelList.fromJson(result['recipes']);
      setState(() {
        _cookInfoList = infoList.list;
      });
    } catch (e) {
      setState(() {});
    } finally {
      setState(() {});
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getRecommendData();
  }

  @override
  Widget build(BuildContext context) {
    if (_cookInfoList.length == 0) {
      return StancePage();
    } else {
      return Container(
          color: Colors.white,
          child: MediaQuery.removePadding(
            context: context,
            removeTop: true,
            child: ListView.builder(

                ///禁用滑动事件
                physics: AlwaysScrollableScrollPhysics(),
                itemCount: _cookInfoList.length,
                itemBuilder: (BuildContext context, int index) {
                  return _itemCell(index);
                }),
          ));
    }
  }

  Widget _itemCell(int index) {
    return InkWell(
      child: Container(
        padding: EdgeInsets.only(left: 15, right: 15, top: 15),
        color: Colors.white,
        child: Row(
          children: [
            ClipRRectCachedImage(
              url: _cookInfoList[index].img!,
              width: 165,
              height: 115,
              radius: 8,
            ),
            Container(
              padding: EdgeInsets.only(
                left: 15,
                top: 15,
              ),
              height: 115,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    child: Text(
                      _cookInfoList[index].n!,
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                      style: TextStyle(
                          fontSize: 15,
                          color: Colors.black,
                          fontWeight: FontWeight.w500),
                    ),
                    width: MediaQuery.of(context).size.width-210,
                  ),
                  SizedBox(
                    height: 8,
                  ),
                  Text(
                    _cookInfoList[index].recommend_label!,
                    style: TextStyle(fontSize: 13, color: Colors.black),
                  ),
                  SizedBox(
                    height: 8,
                  ),
                  Row(
                    children: [
                      IconImage(
                          url: _cookInfoList[index].a!.p!,
                          width: 20,
                          height: 20,
                          radius: 10,
                          borderWidth: 1,
                          borderColor: AppColors.line),
                      SizedBox(
                        width: 8,
                      ),
                      Text(
                        _cookInfoList[index].a!.n!,
                        style: TextStyle(
                            fontSize: 11, color: AppColors.qianTextColor),
                      ),
                    ],
                  )
                ],
              ),
            )
          ],
        ),
      ),
      onTap: () {
        Navigator.of(context).push(MaterialPageRoute(builder: (_) {
          return CookDetailPage(id: _cookInfoList[index].id!);
        }));
      },
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
